package hot100;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;

public class Solution146 {
    class LRUCache {
        int cap;
        LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();


        public LRUCache(int capacity) {
            this.cap = capacity;
        }

        public int get(int key) {
            if (!map.containsKey(key)) return -1;
            makeRecent(key);
            return map.get(key);
        }

        public void put(int key, int value) {
            if (map.containsKey(key)) {
                map.put(key, value);
                makeRecent(key);
                return;
            }
            if (map.size() == cap) {
                int del = map.keySet().iterator().next();
                map.remove(del);
            }
            map.put(key, value);
        }

        public void makeRecent(int key) {
            Integer i = map.get(key);
            map.remove(key);
            map.put(key, i);
        }
    }

}
